## Figure for Voting while Trans
library(readxl)
library(dplyr)
library(ggplot2)

dat <- read_excel("C:/Users/aflor/OneDrive - american.edu/Voting while Transgender/problem_preds.xlsx",
                  sheet = "Sheet3")

dat <- dat %>%
  mutate(trans = factor(trans, levels = c(0,1), labels = c("Cisgender","TGD")))

dat2 <- read_excel("C:/Users/aflor/OneDrive - american.edu/Voting while Transgender/categorical_output_voting_transgender.xlsx", 
                   sheet = "Sheet5")

dat2 <- dat2 %>%
  filter(is.na(trans)==FALSE) %>%
  mutate(trans = factor(trans, levels = c(0,1), labels = c("Cisgender","TGD")))

dat2 <- dat2 %>%
  mutate(lab = case_when(vid==1.5 ~"L",
                         vid==3 ~ "M",
                         vid==4.5 ~ "H"))

## Reorder:
dat <- dat %>%
  mutate(idd = factor(idd, levels = c("Easier to Update ID", "Difficult to Update ID")))

dat2 <- dat2 %>%
  mutate(idd = factor(idd, levels = c("Easier to Update ID", "Difficult to Update ID")))

p <- ggplot(dat, aes(x = vid, y = est, color = trans, fill = trans))

p + geom_ribbon(aes(ymin=lb, ymax = ub, color = NULL), alpha = 0.4) +
  geom_line(size = 1.2) + 
  geom_errorbar(data = dat2, aes(x = vid, y =est, ymin = lb, ymax = ub), width=0.2) +
  geom_text(data = dat2, aes(label = lab), color = "black", size = 3) +
  ggthemes::theme_clean() +
  scale_fill_brewer(type = "qual", palette = 2) +
  scale_color_brewer(type = "qual", palette = 2) +
  theme(legend.background = element_blank(), legend.position = "bottom") +
  labs(x = "Strictess of Voter ID Law",
       y = "Pr(Any Problem)",
       fill = NULL, color = NULL) +
  facet_wrap(~idd)

ggsave("fig1_vwt.tiff", device = "tiff", path="C:/Users/aflor/Downloads/", width = 10, height = 6, units = "in")

dat <- read_excel("C:/Users/aflor/OneDrive - american.edu/Voting while Transgender/problem_preds.xlsx",
                  sheet = "Sheet4")

dat <- dat %>%
  mutate(trans = factor(trans, levels = c(0,1), labels = c("Cisgender","TGD")),
         idd = factor(idd, levels = c("Easier to Update ID", "Difficult to Update ID")),
         lb = est - 1.96*se,
         ub = est + 1.96*se)

p <- ggplot(subset(dat, is.na(lb)==FALSE), aes(x = vid, y = est, color = trans, fill = trans))

p +   geom_ribbon(aes(ymin=lb, ymax = ub, color = NULL), alpha = 0.4) +
  geom_line(size = 1) + 
  ggthemes::theme_clean() +
  scale_fill_brewer(type = "qual", palette = 2) +
  scale_color_brewer(type = "qual", palette = 2) +
  theme(legend.background = element_blank(), legend.position = "bottom") +
  labs(x = "Strictess of Voter ID Law",
       y = "Pr(Any Problem)",
       fill = NULL, color = NULL) +
  facet_grid(group~idd)

ggsave("figs6_vwt.tiff", device = "tiff", path="C:/Users/aflor/Downloads/", width = 8, height = 6, units = "in")

## Age replication:

dat <- read_excel("C:/Users/aflor/OneDrive - american.edu/Voting while Transgender/problem_preds.xlsx",
                  sheet = "Sheet5")

dat <- dat %>%
  mutate(trans = factor(trans, levels = c(0,1), labels = c("Cisgender","TGD")),
         idd = factor(idd, levels = c("Easier to Update ID", "Difficult to Update ID")),
         lb = est - 1.96*se,
         ub = est + 1.96*se)

p <- ggplot(dat, aes(x = vid, y = est, color = trans, fill = trans))

p +   geom_ribbon(aes(ymin=lb, ymax = ub, color = NULL), alpha = 0.4) +
  geom_line(size = 1) + 
  ggthemes::theme_clean() +
  scale_fill_brewer(type = "qual", palette = 2) +
  scale_color_brewer(type = "qual", palette = 2) +
  theme(legend.background = element_blank(), legend.position = "bottom") +
  labs(x = "Strictess of Voter ID Law",
       y = "Pr(Any Problem)",
       fill = NULL, color = NULL) +
  facet_wrap(~idd)

ggsave("figageaux_vwt.tiff", device = "tiff", path="C:/Users/aflor/Downloads/", width = 10, height = 6, units = "in")

## Validated voter matches:

dat <- read_excel("C:/Users/aflor/OneDrive - american.edu/Voting while Transgender/problem_preds.xlsx",
                  sheet = "Sheet6")

dat <- dat %>%
  mutate(trans = factor(trans, levels = c(0,1), labels = c("Cisgender","TGD")),
         idd = factor(idd, levels = c("Easier to Update ID", "Difficult to Update ID")),
         lb = est - 1.96*se,
         ub = est + 1.96*se)

p <- ggplot(dat, aes(x = vid, y = est, color = trans, fill = trans))

p +   geom_ribbon(aes(ymin=lb, ymax = ub, color = NULL), alpha = 0.4) +
  geom_line(size = 1) + 
  ggthemes::theme_clean() +
  scale_fill_brewer(type = "qual", palette = 2) +
  scale_color_brewer(type = "qual", palette = 2) +
  theme(legend.background = element_blank(), legend.position = "bottom") +
  labs(x = "Strictess of Voter ID Law",
       y = "Pr(Any Problem)",
       fill = NULL, color = NULL) +
  facet_wrap(~idd)

ggsave("figageauxvalidated_vwt.tiff", device = "tiff", path="C:/Users/aflor/Downloads/", width = 10, height = 6, units = "in")

## voter non-matches:

dat <- read_excel("C:/Users/aflor/OneDrive - american.edu/Voting while Transgender/problem_preds.xlsx",
                  sheet = "Sheet7")

dat <- dat %>%
  mutate(trans = factor(trans, levels = c(0,1), labels = c("Cisgender","TGD")),
         idd = factor(idd, levels = c("Easier to Update ID", "Difficult to Update ID")))

p <- ggplot(dat, aes(x = vid, y = est, color = trans, fill = trans))

p +   geom_ribbon(aes(ymin=lb, ymax = ub, color = NULL), alpha = 0.4) +
  geom_line(size = 1) + 
  ggthemes::theme_clean() +
  scale_fill_brewer(type = "qual", palette = 2) +
  scale_color_brewer(type = "qual", palette = 2) +
  theme(legend.background = element_blank(), legend.position = "bottom") +
  labs(x = "Strictess of Voter ID Law",
       y = "Pr(Any Problem)",
       fill = NULL, color = NULL) +
  facet_wrap(~idd)

ggsave("figageauxunvalidated_vwt.tiff", device = "tiff", path="C:/Users/aflor/Downloads/", width = 10, height = 6, units = "in")

